$().ready(function() {
	initDictDatas();
	validateRule();
});
//构建字典数据
//构建字典数据
function initDictDatas(){
	
	var thatDictFields = window.parent.dictFields;
	var thatDictkeys = window.parent.dictKeys;
	var thatDictDatas = window.parent.dicDatas;
	
	//省
	$('#provinceNum').bootstrapSelect({
	    url:ctx+'plat/hyBaseArea/list/100000',
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属省",
	    onChange:function(value){
	    	if(value == null || value == ''){
	    		return false;
	    	}
	    	//级联市
	    	$('#cityNum').bootstrapSelect({
	    		url:ctx+'plat/hyBaseArea/list/'+value,
	    		valueField: 'areaNum',
	    		textField: 'areaName',
	    		placeholder: "请选择所属市"
	    	});
	    	//bootstrap-select使用规则是先执行操作然后refresh
	    	$('#cityNum').selectpicker('refresh');
	    }
	});
	$('#cityNum').bootstrapSelect({
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属市"
	});
	$('#category').bootstrapSelect({
		data:thatDictDatas['category'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择主营类目"
	});
	$('#platId').bootstrapSelect({
	    url:ctx+'plat/platInfo/listPlatDict',
	    valueField: 'platId',
	    textField: 'platName',
	    placeholder: "请选择所属平台",
	    onChange:function(value){
	    }
	});
	$('#provinceNum').bootstrapSelect('setValue',provinceNumValue);
	$('#cityNum').bootstrapSelect('setValue',cityNumValue);
	$('#platId').bootstrapSelect('setValue',platId);
	$('#category').bootstrapSelect('setValue',category);
}
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	if (!$("#merchantNum").val()){
		parent.layer.alert("请输入商户编号");
		return ;
	}
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx + "plat/merchantInfo/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}

var mapLayer;
function openMap() {
	var height = $(window).height() - 50;
	mapLayer = layer.open({
		type : 2,
		title : '从地图选择位置',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area: ['935px', '680px'],
		content : ctx+'plat/merchantInfo/testmap' // iframe的url
	});
}
function closeMap(){
	layer.close(mapLayer);
}
function closeEdit(){
	var index = parent.layer.getFrameIndex(window.name);  
    parent.layer.close(index);//关闭当前页
}
function openWxUserView(){
	layer.open({
		type:2,
		title:"选择商户管理员",
		area : [ '800px', '600px' ],
		content:ctx+"plat/merchantInfo/wxUserView"
	})
}
function loadWxUser(openId,headimg){
	$("#openid").val(openId);
	$("#headimg").val(headimg);
	$("#nickName").val(openId);
}